/**
 * 
 */
	$(function () {
		//加载起始数据
		var url = pms_url+"sys/user/list";
		var params = {};
		var obj = load(url,params);
		var ary = obj.user;
		$.each(ary, function(index, u) {
		var tr ="<tr role='row'>"+
		"<td><input id='cb_"+index+"' type='checkbox' class='checkboxes'/></td>"+
		"<td>"+u.user_id+"</td>"+
		"<td>"+u.user_name+"</td>"+
		"<td>"+u.password+"</td>";
		 $("#userlist").find("tbody").append(tr);
		});
		TableDatatablesEditable.init();
		//绑定add单击事件
		$('#btnAdd').on('click', function () {
			$('#modalAdd').find("span[id='lblAddTitle']").html("新增用户"); 
			$('#modalAdd').find("input").val("");
			$('#modalAdd').modal("show");
		});
		
		//绑定del单击事件
		$('#btnDel').on('click', function () {
			if ($("#userlist").find("input[type='checkbox']").is(':checked')) {
				$('#modalDel').modal("show");
			}
			else {
				alert("请选中要删除的用户！");
			}
		});	
		
		//新增新用户或者更新
		$("#btnAddUser").on('click', function () {
			  var bootstrapValidator = $("#userform").data('bootstrapValidator');
			   bootstrapValidator.validate();
			   if(bootstrapValidator.isValid())
			     {	
					var id = $('#modalAdd').find("input[name='updateID']").val();
					var user_id = $('#id').val();
					var name = $('#name').val();
					var psd = $('#psd').val();
				   if(id==""){
					    var params = {user_id:user_id,user_name:name,password:psd};
						var url = oem_url+"sys/user/create";
						$.post(url, params, function(data) {
							if (data.err_code=="0") {
								alert("保存数据成功"+data.err_msg);
								$("#userform input").val("");
								$('#modalAdd').modal("hide");
								$("#pagenum").val("1");
								window.location.reload();
							}
							else {
								alert("保存数据失败"+data.err_msg);
							}
						}, "json");	
				   }
				   else {
					   var params = {user_id:user_id,user_name:name,password:psd};
					   var url = oem_url+"sys/user/update";
					   $.post(url, params, function(data) {
							if (data.err_code=="0") {
								alert("更新数据成功"+data.err_msg);
								$("#userform input").val("");
								$('#modalAdd').modal("hide");
								$("#pagenum").val("1");
								window.location.reload();
							}
							else {
								alert("更新数据失败"+data.err_msg);
							}
						}, "json");
				   }

			     }
			   else return;
		});
		//双击行
		$("#userlist tbody").on('dblclick',"tr", function () {
			var tds = $(this).find("td");
			tds.each(function(index){
				var td = $(this);
				 if(index==1) {
					 $('#modalAdd').find("input[name='updateID']").val(td.text());
					 $('#modalAdd').find("input[name='id']").val(td.text());
				 }
				 if(index==2){
					 $('#modalAdd').find("input[name='name']").val(td.text());
				 }
				 if(index==3){
					 $('#modalAdd').find("input[name='psd']").val(td.text());
				 }
		        $('#lblAddTitle').html("更新人员"); 
		        $('#modalAdd').modal("show");
			});
		});
		
		//删除员工的基本信息
		$('#btnDelUser').on('click', function () {
			var userids="";
			$("#userlist tbody tr").each(function(i){
				if($("#cb_"+i+"").is(':checked')){
					var userid = $("#userlist tr:gt(0):eq("+i+") td:eq(1)").text();
					if(userids=="") userids=userid;
					else userids=userids+","+userid;
				}
			});
			var url = oem_url+"sys/user/delete";
			var params = {userids:userids};
			$.post(url, params, function(data) {
				if (data.err_code=="0") {
					alert("删除数据"+data.err_msg);
					$('#modalDel').modal("hide");
					window.location.reload();
				}
				else {
					alert("删除部门失败"+data.err_msg);
				}
			}, "json");	
		});
		//根据条件查询
		$("#search").on('keypress',function(event){ 
			if(event.keyCode == "13") {
				var page = $("#pagenum").val();
				var con = $("#search").val();
				if(con!=""){
					var url = oem_url+"sys/user/listByCondition";
					var params = {page:page,con:con};
					var obj = load(url,params);
					var ary = obj.user;
					$("#userlist  tr:not(:first)").remove();
					$.each(ary, function(index, u) {
					var tr ="<tr role='row'>"+
					"<td><input id='cb_"+index+"' type='checkbox' class='checkboxes'/></td>"+
					"<td>"+u.user_id+"</td>"+
					"<td>"+u.user_name+"</td>"+
					"<td>"+u.password+"</td>";
					 $("#userlist").find("tbody").append(tr);
					});
				}
				else {
					var page = $("#pagenum").val();
					var url = oem_url+"sys/user/listall";
					var params = {page:page};
					var obj = load(url,params);
					var ary = obj.user;
					$("#userlist  tr:not(:first)").remove();
					$.each(ary, function(index, u) {
					var tr ="<tr role='row'>"+
					"<td><input id='cb_"+index+"' type='checkbox' class='checkboxes'/></td>"+
					"<td>"+u.user_id+"</td>"+
					"<td>"+u.user_name+"</td>"+
					"<td>"+u.password+"</td>";
					 $("#userlist").find("tbody").append(tr);
					});
				}
			}  
		});
		//验证输入格式
			$('#userform').bootstrapValidator({
	            message: 'This value is not valid',
	            feedbackIcons: {/*输入框不同状态，显示图片的样式*/
	                valid: 'glyphicon glyphicon-ok validator',
	                invalid: 'glyphicon glyphicon-remove validator',
	                validating: 'glyphicon glyphicon-refresh validator'
	            },
	            fields: {/*验证*/
	            	id: {/*键名username和input name值对应*/
	                    message: 'The name is not valid',
	                    validators: {
	                        notEmpty: {/*非空提示*/
	                            message: '工号不能为空'
	                        },
	                        stringLength: {
	                            min: 8,
	                            max: 8,
	                            message: '工号长度为8位'
	                        }
	                    }
	                },name: {
	                    message: 'The name is not valid',
	                    validators: {
	                        notEmpty: {
	                            message: '姓名不能为空'
	                        }
	                    }
	                }, psd: {
	                    message: 'The ename is not valid',
	                    validators: {
	                        notEmpty: {
	                            message: '密码不能为空'
	                        }
/*	                ,
	                        regexp: { 只需加此键值对，包含正则表达式，和提示 
	                            regexp: /^[a-zA-Z]+$/,
	                            message: '只能是字母'
	                        }*/
	                    }
	                }
	            }
			});
	});
	var TableDatatablesEditable=
		function(){
		var e=function(){
			var a=$("#userlist"),
			l=a.dataTable({lengthMenu:[[10,15,20,-1],[10,15,20,"All"]],pageLength:10,language:{lengthMenu:" _MENU_ records"},columnDefs:[{orderable:!0,targets:[0]},{searchable:!0,targets:[0]}],order:[[0,"asc"]]});
			};
								
			return{init:function(){e()}}}();